import slider from "../../engines/components/slider";
import gameConfig from "../../engines/configs/gameConfig";
import O1HD, { services } from "../../engines/gameEngine";
import { CsvFileKind } from "../../engines/libs/CsvFileKind";
import data_control from "../../engines/libs/data_control";
import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import { REWARD_TYPE } from "../../engines/services/userContent/userInterface";
import PageBase from "../Base/PageBase";
import { PAGE } from "../Enum";
import { INormalLevelStruct } from "../Interface";
import mainRoot from "../MainRoot";

const {ccclass, property} = cc._decorator;

@ccclass
export default class peopleArcheryStartPage extends PageBase {

    @property({type:cc.Node,tooltip:"角色节点"})
    roleNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"选关节点"})
    selectLevelNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"排行榜按钮"})
    rankNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"体力文本"})
    lbPower:cc.Label = null;

    @property({type:cc.Label,tooltip:"金币文本"})
    lbMoney:cc.Label = null;

    levelData :INormalLevelStruct = {
        mode:0,
        subPower:2,
        //@ts-ignore
        levelImg:"jjj_xuanguan_5",
        bundleName: "normalMode",
        name: "level_abb_00002",
        level:5,
        levelType:0,
        gameAloneBundle: "level_abb_00002",
        tips: "下",
        title: "第x关",
        winText: "救命，AI绘画把我画成狗了！",
        loseText: "救命，AI绘画把我画成狗了！",
        levelTitle: "普通关卡",
        infinityFlag:true,
        levelNum:1,
    }

    start(){
        this.refrenshRole();
        O1HD.on("peopleArcheryStartPage.refrenshRole",this.refrenshRole,this);
        this.refrenshPower();
        // let max:number = 
        this.selectLevelNode.getComponent(slider).loadSlider(O1HD.getManagers().peopleArcheryManager.getData().curInfinityLevel - 2,O1HD.getManagers().peopleArcheryManager.getData().curInfinityLevel,0,Object.keys(O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryInfinityEnemyHp,"1")).length-1);
    }

    onBtnSelectRole(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        mainRoot.UISystem.OpenPage(PAGE.PEOPLE_ARCHERY_SELECT_ROLE_PAGE);
    }

    onBtnRank(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        O1HD.getServices().miniGameSrv.getAppApiInstance().showRank();
    }

    refrenshPower(){
        let needPower:number = this.levelData["subPower"] || 1;
        this.lbPower.string = `-${needPower}`;
    }

    refrenshRole(){
        let data = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryRole,O1HD.getManagers().peopleArcheryManager.getData().curUseRoleId);
        let rPath = data["role_path"];
        let sp:cc.Sprite = this.roleNode.getComponent(cc.Sprite);
        mainRoot.frameBundle.load(`Img/Page/selectRole/${rPath}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
            if(err){
                console.log("加载角色图片失败",err);
                return;
            }
            sp.spriteFrame = spf;
        });
    }

    async onBtnStart(){
        let needPower:number = this.levelData["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower()- needPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        mainRoot.GotTips = false;
        mainRoot.GotAnswer = false;
        let levelIndex = this.selectLevelNode.getComponent(slider).getCurSelectIndex();
        this.levelData.levelNum = levelIndex;
        O1HD.getManagers().peopleArcheryManager.setData({curInfinityLevel:levelIndex},false);
        if(this.levelData.levelNum > O1HD.getManagers().peopleArcheryManager.getData().infinityLevel){
            O1HD.getModules().uiControlModule.showMessageBox("未解锁");
            return;
        }
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, 1).bundleName, GAME_MODE.NORMAL, this.levelData.levelNum,true,this.levelData["infinityFlag"] ? this.levelData : null);
        mainRoot.UISystem.ClosePage(PAGE.PEOPLE_ARCHERY_START_PAGE);
        services().userSrv.setPower(-needPower,true,true);
        mainRoot.UISystem.ClosePage(PAGE.LEVELMENU);
        mainRoot.UISystem.ClosePage(PAGE.MODEMENU);
    }

    protected update(dt: number): void {
        this.lbMoney.string = data_control.getNumberFormat(O1HD.getManagers().peopleArcheryManager.getData().money);
    }

}
